<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Validator;


/**
* 商品图片
*/
class ImageController extends Controller
{
    /**
     * 展示当前模块下商品图片列表
     *
     * @return \Illuminate\Http\Response
     */
    public function getIndex(Request $request)
    {
        //
        $ve_id = $request -> input('ve_id');
        $gid = $request -> input('gid');

        //查询所有数据
        $data = DB::table('goods_image') -> where('ve_id', $ve_id) -> get();

        return view('admin/image/index',['data' => $data, 'gid' => $gid, 've_id' => $ve_id]);
    }

    /**
     * 新增图片模板
     *
     * @return \Illuminate\Http\Response
     */
    public function getAdd(Request $request)
    {
        //接收数据
        $gid = $request -> input('gid');
        $ve_id = $request -> input('ve_id');

        //返回上传界面
        return view('admin/image/add', ['gid' => $gid, 've_id' => $ve_id]);
    }

    /**
     * 添加商品图片
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function postInsert(Request $request)
    {
        //
        $input = $request -> except('_token');
        $atime = $request -> input('atime');
        $ve_id = $request -> input('ve_id');
        $gid = $request -> input('gid');

        //设置过滤规则
        $rules = [
            'atime' => 'required',
            'pic' => 'required',
        ];
        
        //设置错误消息
        $messages = [
                 'atime.required' => '图片位置必选',
                'pic.required'  => '没有图片被上传',
        ];

        //验证数据
        $validator = Validator::make($input,$rules,$messages);
       
        if ($validator->fails()) {
            return back()
                        ->withErrors($validator)
                        ->withInput();
        }

       //检测图片是否可用
        if (!$request->hasFile('pic')) {
            return back()
                        ->withErrors('没有图片上传')
                        ->withInput();
        }

        //检验上传的文件是否有效.
        $file = \Input::file('pic');
        if($file -> isValid()){
            
            $clientName = $file -> getClientOriginalName();//客户端文件名称..
            $clientType = $file -> getClientOriginalExtension();
            $arrType = ['jpg','png','git','jpeg'];
            if (!in_array($clientType, $arrType)) {
                return back();
            }
        }
       
        //保存图片并获得路径 
        $filename = mt_rand(1000,9999).time().'.'.$file -> getClientOriginalExtension();
        $res = $file -> move('public/image/poster/',$filename);
        if (!$res) {
            return back();
        }
        
        //获得所需插入数据库数据
        $path = '/public/image/poster/'.$filename;

        $data['path'] = $path;
        if ($atime == 0) {
            $data['atime'] = 0;
        }else{
            $data['atime'] = time();
        }
        $data['ve_id'] = $ve_id;
        //插入数据库
        $res = DB::table('goods_image') -> insert($data);

        //返回结果
        return self::res($res,'添加商品图片','/admin/image/index/?gid='.$gid.'&ve_id='.$ve_id,'/admin/image/add?gid='.$gid.'&ve_id='.$ve_id);
    }

    /**
     * 删除商品图片
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function getDelete(Request $request, $id)
    {
        //
        $gid = $request -> input('gid');
        $ve_id = $request -> input('ve_id');
        $res = DB::table('goods_image') -> where('id', $id) -> delete();
    
        //
        return self::res($res,'删除商品图片','/admin/image/index/?gid='.$gid.'&ve_id='.$ve_id,'/admin/image/index?gid='.$gid.'&ve_id='.$ve_id);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
    * 处理结果函数
    */
     static public function res($res,$message,$url_su,$url_er){
        if ($res) {
         
           //跳转到员工列表并显示成功信息
           return redirect($url_su)
                        ->with(['message'=>$message.'成功','type'=>1]);
        }else{

           //跳转到添加页面
           return redirect($url_er)
                        ->with(['message'=>$message.'失败','type'=>2]);
        }
    }
}
